package book;


import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import sale.SaleManager;

import bookshopSM.BookshopGUI;

public class BookInstock extends JPanel{
    private JButton clearButton, writeButton;
    JLabel btitle;
    private BookshopGUI userInterface;
    String names1[] = { "图 书 编 号","数      量" };
    public BookInstock(){
        btitle = new JLabel("图书入库      ");
        btitle.setFont(new   java.awt.Font("楷体", 1, 20));
        userInterface = new BookshopGUI( names1 );
        Box box=Box.createVerticalBox();
        box.add(box.createVerticalStrut(15));
        box.add(btitle);
        box.add(box.createVerticalStrut(15));
        box.add(userInterface);
        this.add( box );
        writeButton =userInterface.getDoTask1Button();
        writeButton.setText( "保存" );
        writeButton.addActionListener(
                new ActionListener() {
                    public void actionPerformed( ActionEvent event )
                    {   String fieldValues[] = userInterface.getFieldValues();
                        try {
                            int bno1 = Integer.parseInt(fieldValues[0] );
                            int inum1 = Integer.parseInt(fieldValues[1] );
                            String sqlString = "INSERT INTO instock(bno,inum) " +
                                    "VALUES ("+ bno1+","+inum1+  ")";
                            SaleManager studentmanager=new  SaleManager();
                            int result=studentmanager.execUpdate(sqlString);
                            if (result!=0) {
                                userInterface.clearFields();
                                JOptionPane.showMessageDialog( BookInstock.this,
                                        "入库成功", "Insert Result",
                                        JOptionPane.INFORMATION_MESSAGE );
                            }

                        }
                        catch ( NumberFormatException formatException ) {
                            JOptionPane.showMessageDialog( BookInstock.this,
                                    "请输入正确图书编号", "Invalid Number Format",
                                    JOptionPane.ERROR_MESSAGE );
                        }
                        catch (SQLException ee)
                        { System.out.println(ee);	}
                    }
                }
        );

        clearButton = userInterface.getDoTask2Button();
        clearButton.setText( "清除" );
        clearButton.addActionListener(
                new ActionListener() {
                    public void actionPerformed( ActionEvent event )
                    {
                        userInterface.clearFields();
                    }
                }
        );
    }

}
